﻿

/*******************************************************************************
 * Copyright © 2017 FriendStar.Framework 版权所有
 * Author: HYF
 * Description: FriendStar快速开发平台
 * Website：http://www.FriendStar.cn
*********************************************************************************/
using System.Collections.Generic;
using System.Linq;
using SqlSugarRepository;
using FriendStar.Code;
using FriendStar.Domain.Entity.MainData;
using FriendStar.Domain.IRepository.MainData;
using FriendStar.Repository.MainData;
using System;

namespace FriendStar.Application.MainData
{
    public class CPK_ZD_12_DatStoreHouseSetApp
    {
        private string UseUnit = Configs.GetValue("UseUnit"); 

        private ICPK_ZD_12_DatStoreHouseSetRepository service = new CPK_ZD_12_DatStoreHouseSetRepository();
        public List<CPK_ZD_12_DatStoreHouseSetEntity> GetList(string keyword)
        {
            var expression = ExtLinq.True<CPK_ZD_12_DatStoreHouseSetEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.FStoreSetNO.Contains(keyword));
            }
            return service.IQueryable(expression).OrderBy(t => t.FStoreNO).ToList();
        }

        public CPK_ZD_12_DatStoreHouseSetEntity GetForm(string keyValue)
        {
            var expression = ExtLinq.True<CPK_ZD_12_DatStoreHouseSetEntity>();
            expression = expression.And(t => t.FStoreSetID == keyValue);
            return service.FindEntity(keyValue);
        }

        public void DeleteForm(string keyValue)
        {
            service.Delete(t => t.FStoreSetID == keyValue);
        }

        public void SubmitForm(CPK_ZD_12_DatStoreHouseSetEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Update(entity);
            }
            else
            {
                entity.FStoreSetID = Common.GuId();
                entity.UseUnit = UseUnit;
                entity.FCreateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FCreateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FCreateTime = DateTime.Now.ToString("HH:mm:ss");
                entity.FUpdateBy = OperatorProvider.Provider.GetCurrent().UserName;
                entity.FUpdateDate = DateTime.Now.ToString("yyyy-MM-dd");
                entity.FUpdateTime = DateTime.Now.ToString("HH:mm:ss");
                service.Insert(entity);
            }
        }

        public bool CheckedEntity(CPK_ZD_12_DatStoreHouseSetEntity entity)
        {
            var expression = ExtLinq.True<CPK_ZD_12_DatStoreHouseSetEntity>();
            expression = expression.And(t => t.UseUnit == UseUnit);
            expression = expression.And(t => t.FStoreSetNO == entity.FStoreSetNO);
            if (!string.IsNullOrEmpty(entity.FStoreSetID))
            {
                expression = expression.And(t => t.FStoreSetID != entity.FStoreSetID);
            }
            var oldEntityList = service.FindList(expression);
            return oldEntityList.Count == 0;
        }

        public List<CPK_ZD_12_DatStoreHouseSetEntity> StoreHouseSets(string useUnit, string storeNO)
        {
            var expression = ExtLinq.True<CPK_ZD_12_DatStoreHouseSetEntity>();
            if (!string.IsNullOrEmpty(useUnit))
            {
                expression = expression.And(t => t.UseUnit == useUnit);
            }
            if (!string.IsNullOrEmpty(storeNO))
            {
                expression = expression.And(t => t.FStoreNO == storeNO);
            }
            return service.IQueryable(expression).Select(p => new CPK_ZD_12_DatStoreHouseSetEntity() { FStoreSetNO = p.FStoreSetNO }).OrderBy(t => t.FStoreSetNO).ToList();
        }

    }
}
